package com.ux.example.particle
{
	import com.ux.effect.particle.Particle;
	import com.ux.manager.StageManager;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class ParticleExample extends Sprite
	{
		
		private var pList : Vector.<Particle>;
		private var numOfParticle : Number = 1;
		
		public function ParticleExample()
		{
			super();
			
			
			StageManager.stage = this.stage;
			
			pList = new Vector.<Particle>();
			
			this.addEventListener( Event.ENTER_FRAME, onEnter );
			StageManager.stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown );
			StageManager.stage.addEventListener(MouseEvent.MOUSE_UP, onUp );
		}
		
		private function onDown( e:MouseEvent ):void
		{
			numOfParticle = 20;
		}
		
		private function onUp( e:MouseEvent ):void
		{
			numOfParticle = 1;
		}
		
		private function onEnter( e:Event ):void
		{
			
			var len:int = numOfParticle;
			for (var j:int = 0; j < len; j++) 
			{
				var p : Particle = new Particle( SpriteClass, this, StageManager.stage.mouseX, StageManager.stage.mouseY );
				pList.push( p );
				
				p.setVel( randRange(-10,+10), randRange(-10,+10) );
				
				//gravity
				p.gravity = 1;
				p.shrink = 0.95;
				p.drag = 0.99;
				p.setScale( randRange( 1, 2 ) );
			}
			
			
			while( pList.length > 400 )
			{
				pList.shift().destroy();
			}
			
			for(var i:Number = 0;i<pList.length;i++)
			{
				// and update each one
				pList[i].update();
				
				
				
				if( pList[i].clip.y > 350 )
				{
					pList[i].clip.y = 350;
					pList[i].yVel = -pList[i].yVel*0.5;
				}
			}
			
			
		}
		
		
		
		private function randRange(min:Number, max:Number) : Number
		{
			var randomNum:Number = (Math.random() * (max - min )) + min;
			return randomNum;
		}
	}
}






